package com.task.controller.system;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.task.common.annotation.Log;
import com.task.common.entity.custom.Item;
import com.task.common.entity.custom.model.QueryItemModel;
import com.task.common.entity.sys.Permission;
import com.task.common.entity.sys.Role;
import com.task.common.entity.sys.model.permission.PermissionCreateModel;
import com.task.common.entity.sys.model.permission.PermissionQueryModel;
import com.task.common.entity.sys.model.role.RoleCreateModel;
import com.task.common.enums.BusinessType;
import com.task.common.util.body.JsonResult;
import com.task.common.util.body.ResultTool;
import com.task.common.util.model.ModelUtil;
import com.task.service.IPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Huzz
 * @since 2022-05-21
 */
@Api(tags = "用户权限管理")
@RestController
@RequestMapping("/sys/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @Log(title = "操作日志", businessType = BusinessType.INSERT, isSaveResponseParam = true)
    @ApiOperation(value = "添加权限")
    @PostMapping("/addPermission")
    public JsonResult addPermission(@RequestBody PermissionCreateModel model){
        return permissionService.addPermission((Permission) ModelUtil.toEntityStatic(model, Permission.class)) ? ResultTool.success() : ResultTool.fail();
    }

    @ApiOperation("权限列表")
    @Log(title = "权限列表",businessType = BusinessType.SELECT,isSaveRequestParam = true)
    @GetMapping("/findPermissionList")
    public JsonResult<IPage> findPermissionList(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSie,
                                          PermissionQueryModel model){
        return ResultTool.success(permissionService.findPermissionList(pageNum, pageSie, (Permission) ModelUtil.toEntityStatic(model, Permission.class)));
    }

    @Log(title = "操作日志", businessType = BusinessType.INSERT, isSaveResponseParam = true)
    @ApiOperation(value = "权限分配")
    @PostMapping("/permissionDistribute")
    public JsonResult permissionDistribute(@RequestParam Integer permissionId, @RequestParam Integer roleId){
        return permissionService.permissionDistribute(permissionId, roleId) ? ResultTool.success() : ResultTool.fail();
    }
}
